/*
 * 文件名：CarBillDetailServiceImpl.java 描述： 修改人：Administrator 修改时间：2016年4月25日 跟踪单号： 修改单号： 修改内容：
 */

package com.zcsy.manage.service.property.impl;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zcsy.commons.constants.Constants;
import com.zcsy.manage.model.pay.alipay.AliPayResult;
import com.zcsy.manage.model.pay.weixin.PayOrderResultResponse;
import com.zcsy.manage.model.property.CarBill;
import com.zcsy.manage.model.property.CarBillDetail;
import com.zcsy.manage.persistence.property.CarBillDetailDao;
import com.zcsy.manage.service.property.ICarBillDetailService;
import com.zcsy.manage.service.wxpay.service.WxPayResult;


@Service
public class CarBillDetailServiceImpl implements ICarBillDetailService {

    @Autowired
    private CarBillDetailDao dao;

    /*
     * (non-Javadoc)
     * @see
     * com.zcsy.manage.service.property.ICarBillDetailService#insert(com.zcsy.manage.model.property
     * .CarBillDetail)
     */
    @Override
    public int insert(CarBillDetail record) {
        return dao.insert(record);
    }

    /*
     * (non-Javadoc)
     * @see
     * com.zcsy.manage.service.property.ICarBillDetailService#updateByPrimaryKey(com.zcsy.manage
     * .model.property.CarBillDetail)
     */
    @Override
    public int updateByPrimaryKey(CarBillDetail record) {
    	CarBillDetail olddetail = dao.selectByPrimaryKey(record.getId());
    	
    	olddetail.setBillType(record.getBillType());
    	olddetail.setBillTitle(record.getBillTitle());
    	olddetail.setBillNo(record.getBillNo());
    	olddetail.setOperator(record.getOperator());
    	olddetail.setOperatorAccount(record.getOperatorAccount());
    	olddetail.setUpdateTime(record.getUpdateTime());
    	
        return dao.updateByPrimaryKey(olddetail);
    }

    /*
     * (non-Javadoc)
     * @see
     * com.zcsy.manage.service.property.ICarBillDetailService#deleteByPrimaryKey(java.lang.String)
     */
    @Override
    public int deleteByPrimaryKey(String id) {
        return dao.deleteByPrimaryKey(id);
    }

    /*
     * (non-Javadoc)
     * @see
     * com.zcsy.manage.service.property.ICarBillDetailService#selectByPrimaryKey(java.lang.String)
     */
    @Override
    public CarBillDetail selectByPrimaryKey(String id) {
        return dao.selectByPrimaryKey(id);
    }
    
    @Override
    public CarBillDetail selectBycarID(String carID) {
    	return dao.selectBycarID(carID);
    }

    /*
     * (non-Javadoc)
     * @see com.zcsy.manage.service.property.ICarBillDetailService#selectLastBill(java.lang.String)
     */
    @Override
    public CarBillDetail selectLastBill(Map<String, Object> params) {
        return dao.selectLastBill(params);
    }

    /*
     * (non-Javadoc)
     * @see com.zcsy.manage.service.property.ICarBillDetailService#selectByCarNo(java.lang.String)
     */
    @Override
    public List<CarBillDetail> selectByCarNo(String CarNo) {
        return dao.selectByCarNo(CarNo);
    }

    /*
     * (non-Javadoc)
     * @see
     * com.zcsy.manage.service.property.ICarBillDetailService#selectByKeyword(com.zcsy.manage.model
     * .property.CarBillDetail)
     */
    @Override
    public List<CarBill> selectByKeyword(CarBillDetail record) {
        return dao.selectByKeyword(record);
    }

    @Override
    public List<CarBillDetail> selectCarBillDetailByHouseId(List<String> housesID) {
    	return dao.selectCarBillDetailByHouseId(housesID);
    }
    
    @Override
    public CarBillDetail getCarWithProperty(String carNo) {
    	return dao.getCarWithProperty(carNo);
    }
    
    @Override
    public void payCarBill(String billId , Object payResult){
    	CarBillDetail detail = selectByPrimaryKey(billId);
    	if(detail.getState().intValue() == Constants.AFTER_STATE
    			|| detail.getState().intValue() == Constants.BEFORE_STATE){
    		detail.setPayTime(new Date());
    		detail.setUpdateTime(new Date());
    		detail.setState(Constants.AFTER_STATE);//已缴费未同步
            if(payResult instanceof PayOrderResultResponse) {
            	//微信结果
            	PayOrderResultResponse result = (PayOrderResultResponse)payResult;
            	detail.setPayWay(Constants.PAY_WAY_WXPAY);
            	detail.setPaySystem(Constants.PAY_SYSTEM_WEB);
            	detail.setExternalRespCode(result.getResult_code());
                detail.setExternalTradeNo(result.getOut_trade_no());
                //微信支付返回的金额是以分为单位，所以要除以100
                detail.setTotalFee(
                		new BigDecimal(result.getTotal_fee()).divide(new BigDecimal(100)));
            }else if(payResult instanceof AliPayResult){
            	//支付宝结果
            	AliPayResult result = (AliPayResult)payResult;
            	detail.setPayWay(Constants.PAY_WAY_ALIPAY);
            	detail.setPaySystem(result.getPaySystem());
                detail.setExternalRespCode(result.getExternal_resp_code());
                detail.setExternalTradeNo(result.getExternal_trade_no());
                detail.setTotalFee(result.getTotal_fee());
            }else if(payResult instanceof WxPayResult){
            	//微信app支付
            	WxPayResult result = (WxPayResult)payResult;
            	detail.setPayWay(Constants.PAY_WAY_WXPAY);
            	detail.setPaySystem(result.getPaySystem());
                detail.setExternalRespCode(result.getResultCode());
                detail.setExternalTradeNo(result.getOutTradeNo());
                //微信支付返回的金额是以分为单位，所以要除以100
                detail.setTotalFee(
                		new BigDecimal(result.getTotalFee()).divide(new BigDecimal(100)));
            }
            
            updateByPrimaryKey(detail);
            
            //此时调用海康系统更新缴费有效期
//            vo = InvokeHkWebserviceApi.pushLeaseInteface(time, carNo);
//            if(vo.isSuccess()) {
//                detail.setState(Constants.CAR_PAY_SUCCESS);
//                updateByPrimaryKey(detail);
//            } else {
//                
//            }
    	}
    }
}
